<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>燃油附加费税</title>
    </head>
    <body>
        <div id="fuelTaxsGrid" style="height:580px;" class="x-form-item">
        </div>
        <script>
            Ext.onReady(function(){
                var fuelTaxCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    header: '编号',
                    dataIndex: 'id'
                }, {
                    header: '城市对',
                    dataIndex: 'fromto'
                }, {
                    header: '公里数',
                    dataIndex: 'km'
                }, {
                    header: '费税',
                    dataIndex: 'tax'
                }]);
                //隐藏系统编号
                fuelTaxCM.setHidden(1, !fuelTaxCM.isHidden(1));
                var fuelTaxDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/admin/getAllFuelTaxs'
                    }),
                    reader: new Ext.data.JsonReader({
                        totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                        name: 'id'
                    }, {
                        name: 'fromto'
                    }, {
                        name: 'km'
                    }, {
                        name: 'tax'
                    }])
                });
                /************************************************************
                 * Define menubar now in here
                 *   add and delete functions
                 ************************************************************/
                var menubar = [{
                    text: '增加燃油税',
                    tooltip: '增加燃油税',
                    iconCls: 'addItem',
                    handler: function(){
                        // add new planetax now - action
                        CreateFuelTax(fuelTaxDS);
                    }
                }, '-', {
                    text: '删除燃油税',
                    tooltip: '删除燃油税',
                    iconCls: 'remove',
                    handler: function(){
                        // delete planetax now - action
                        DeleteFuelTax(fuelTaxDS, fuelTaxGridPanel);
                    }
                }, '-', {
                    text: '编辑燃油税',
                    tooltip: '编辑燃油税',
                    iconCls: 'editItem',
                    handler: function(){
                        // delete planetax now - action
                        EditFuelTax(fuelTaxDS, fuelTaxGridPanel);
                    }
                }];
                var fuelTaxGridPanel = new Ext.grid.GridPanel({
                    el: 'fuelTaxsGrid',
                    ds: fuelTaxDS,
                    cm: fuelTaxCM,
                    //sm: sm,
                    collapsible: true,
                    animCollapse: false,
                    title: '燃油税费列表',
                    tbar: menubar,
                    loadMask: true
                });
                fuelTaxGridPanel.render();
                //ds.load();
                fuelTaxDS.load();
                
            });
            
            var airportTpl;
            var fuel_from_port;
            var fuel_from_code;
            var fuel_to_port;
            var fuel_to_code;
            var km;
            var fuelTax;
            function init_fuel(){
                airportTpl = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{airport_name}({airport_code})', '</div></tpl>');
                fuel_from_port = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                            name: 'id'
                        }, {
                            name: 'airport_name'
                        }, {
                            name: 'airport_code'
                        }])
                    }),
                    fieldLabel: '始发地',
                    name: 'from_port',
                    //hiddenName: 'from_id',
                    allowBlank: false,
                    blankText: "始发地必须选择",
                    //displayField: 'airport_name',
                    //valueField: 'id',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    //width: 230,
                    anchor: '90%',
                    //pageSize: 10,
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        fuel_from_port.setValue(record.data.airport_name);
                        fuel_from_code.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                fuel_to_port = new Ext.form.ComboBox({
                    store: new Ext.data.Store({
                        proxy: new Ext.data.HttpProxy({
                            url: "/admin/getAllAirportComboBox"
                        }),
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, [{
                            name: 'id'
                        }, {
                            name: 'airport_name'
                        }, {
                            name: 'airport_code'
                        }])
                    }),
                    fieldLabel: '目的地',
                    name: 'to_port',
                    //hiddenName: 'to_id',
                    allowBlank: false,
                    blankText: "目的地必须选择",
                    displayField: 'airport_name',
                    valueField: 'id',
                    typeAhead: false,
                    loadingText: '正在查找...',
                    //width: 230,
                    anchor: '90%',
                    //pageSize: 10,
                    hideTrigger: false,
                    tpl: airportTpl,
                    minChars: 0,
                    itemSelector: 'div.search-item',
                    onSelect: function(record){
                        fuel_to_port.setValue(record.data.airport_name);
                        fuel_to_code.setValue(record.data.airport_code);
                        if (this.isExpanded()) {
                            this.collapse();
                        }
                    }
                });
                fuel_from_code = new Ext.form.Hidden({
                    name: 'from',
                    value: ''
                });
                fuel_to_code = new Ext.form.Hidden({
                    name: 'to',
                    value: ''
                });
                km = new Ext.form.NumberField({
                    fieldLabel: '公里数',
                    allowBlank: false,
                    name: 'km',
                    anchor: '90%'
                });
                fuelTax = new Ext.form.NumberField({
                    fieldLabel: '税费',
                    allowBlank: false,
                    name: 'tax',
                    anchor: '90%'
                });
            }
            
            function CreateFuelTax(dataStore){
                init_fuel();
                
                var addFormPanel = new Ext.form.FormPanel({
                    baseCls: 'x-plain',
                    labelWidth: 65,
                    url: '/admin/addFuelTax',
                    items: [fuel_from_port, fuel_to_port, fuel_from_code, fuel_to_code, km, fuelTax]
                });
                
                var addFuelTaxWindow = new Ext.Window({
                    title: '增加燃油税',
                    width: 300,
                    height: 200,
                    layout: 'fit',
                    plain: true,
                    bodyStyle: 'padding:5px;',
                    buttonAlign: 'center',
                    items: addFormPanel,
                    modal: true,
                    buttons: [{
                        text: '增加',
                        handler: function(){
                            if (addFormPanel.form.isValid()) {
                                if (fuel_from_code.getValue() == "") {
                                    Ext.MessageBox.alert('提示', '请选择始发城市');
                                }
                                else if (fuel_to_code.getValue() == "") {
                                    Ext.MessageBox.alert('提示', '请选择目的城市');
                                }
                                else {
                                    addFormPanel.form.submit({
                                        waitMsg: 'In processing',
                                        success: function(form, action){
                                            Ext.MessageBox.alert('提示', '提交成功');
                                            addFuelTaxWindow.destroy();
                                            dataStore.load();
                                        },
                                        failure: function(form, action){
                                            Ext.MessageBox.alert('提交错误消息', '提交失败');
                                        }
                                    });
                                }
                            }
                            else {
                                Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                            }
                        }
                    }, {
                        text: '取消',
                        handler: function(){
                            addFuelTaxWindow.destroy();
                        }
                    }]
                });
                
                addFuelTaxWindow.show();
            };
            //----------------Delete FuelTax--------------------------
            function DeleteFuelTax(dataStore, gridPanel){
                // get the selected items
                var m = gridPanel.getSelections();
                if (m.length > 0) {
                    Ext.MessageBox.confirm('提示', '你真的要删除这些记录吗？', function(btn){
                        if (btn == 'yes') {
                            var jsonData = "[";
                            for (var i = 0, len = m.length; i < len; i++) {
                                var ss = "{\"id\":\"" + m[i].get("id") + "\"}";
                                if (i == 0) 
                                    jsonData = jsonData + ss;
                                else 
                                    jsonData = jsonData + "," + ss;
                                dataStore.remove(m[i]);
                            }
                            jsonData = jsonData + "]";
                            //Ext.MessageBox.alert(jsonData);
                            dataStore.load({
                                params: {
                                    delData: jsonData
                                }
                            });
                        }
                    });
                }
                else {
                    Ext.MessageBox.alert('错误提示', '请至少选择一个记录删除');
                }
            };
            
            //------------Edit FuelTax-----------------
            function EditFuelTax(dataStore, gridPanel){
                //Ext.MessageBox.alert('提示',selectedId);
                var m = gridPanel.getSelections();
                if (m.length > 0 && m.length == 1) {
                    var selectedId = m[0].get("id");
                    init_fuel();
                    var fromTo = new Ext.form.TextField({
                        fieldLabel: '城市对',
                        allowBlank: true,
                        readOnly: true,
                        name: 'fromto',
                        anchor: '90%'
                    });
                    var editFormPanel = new Ext.form.FormPanel({
                        baseCls: 'x-plain',
                        labelWidth: 65,
                        url: '/admin/editFuelTax?id=' + selectedId,
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, ['id', 'fromto', 'km', 'tax']),
                        
                        items: [fromTo, km, fuelTax]
                    });
                    editFormPanel.form.load({
                        url: '/admin/getFuelTaxByID?id=' + selectedId,
                        waitMsg: '正在载入数据...',
                        success: function(form, action){
                            //Ext.MessageBox.alert('提示信息', '信息加载成功！');
                        },
                        failure: function(form, action){
                            Ext.MessageBox.alert('提示信息', '信息加载失败');
                        }
                    });
                    
                    // define window and show it in desktop
                    var editFuelTaxWindow = new Ext.Window({
                        title: '编辑',
                        width: 300,
                        height: 160,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: editFormPanel,
                        modal: true,
                        buttons: [{
                            text: '提交',
                            handler: function(){
                                // check form value 
                                if (editFormPanel.form.isValid()) {
                                    editFormPanel.form.submit({
                                        params: {
                                            id: selectedId
                                        },
                                        waitMsg: '处理过程中...',
                                        failure: function(form, action){
                                            //debugger;
                                            Ext.MessageBox.alert('错误提示', action.failureType);
                                        },
                                        success: function(form, action){
                                            Ext.MessageBox.alert('确认', '提交成功');
                                            editFuelTaxWindow.hide();
                                            dataStore.load();
                                        }
                                    });
                                }
                                else {
                                    Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                                }
                            }
                        }, {
                            text: '取消',
                            handler: function(){
                                editFuelTaxWindow.hide();
                            }
                        }]
                    });
                    editFuelTaxWindow.show();
                }
                else {
                    Ext.MessageBox.alert('提示', "请先选择一条要编辑记录");
                }
            };
        </script>
    </body>
</html>
